package soderqvist.paulsson.logmaster;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddNote extends Activity implements OnClickListener,
		OnItemSelectedListener {
	private String[] names = { "Select user", "Erik", "Johan" };
	private Button button;
	private Spinner spinner;
	private EditText editText;
	private String selectedUser = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_note);
		button = (Button) findViewById(R.id.addNoteButton);
		button.setOnClickListener(this);

		spinner = (Spinner) findViewById(R.id.addNoteSpinner);
		spinner.setOnItemSelectedListener(this);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, names);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		editText = (EditText) findViewById(R.id.addNoteInput);

	}

	protected void noteAdded() {
		Log.i("ADDNOTE", "Note sent to server!");
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_add_note, menu);
		return true;
	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.addNoteButton) {
			if (!selectedUser.equals("Select user")) {
				if (editText.getText().toString().length() > 0) {
					MyNewThread thread = new MyNewThread();
					thread.execute(selectedUser + "\n"
							+ editText.getText().toString());
				} else {
					Toast.makeText(this, "Please enter a note.",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(this, "Please select a user.",
						Toast.LENGTH_SHORT).show();
			}
		}

	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		selectedUser = names[arg2];
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		selectedUser = "Select user";
	}

	protected void makeErrorToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	private class MyNewThread extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... note) {
			try {
				Socket clientSocket = new Socket("85.24.188.49", 41253);
				InputStreamReader inputStreamReader = null;
				BufferedReader input = null;
				PrintStream output = null;
				try {
					inputStreamReader = new InputStreamReader(
							clientSocket.getInputStream());
					input = new BufferedReader(inputStreamReader);
					output = new PrintStream(clientSocket.getOutputStream());
					output.println("log-master message");
					output.println(note[0]);
					output.println("NOHOMO");
					output.flush();
					String lineRead = "";
					boolean success = false;
					while ((lineRead = input.readLine()) != null) {
						if (lineRead.contains("EY OK")) {
							success = true;
							break;
						}
					}
					output.close();
					input.close();
					inputStreamReader.close();
					clientSocket.close();
					if (success) {
						noteAdded();
					}
				} catch (IOException e) {
					makeErrorToast("Could not send request to server, try again later.");
				}
			} catch (UnknownHostException e) {
				makeErrorToast("Could not establish connection to the server, contact your server administator.");
			} catch (IOException e) {
				makeErrorToast("Port in use, reprogram the app if this happens.. :P");
			}
			return null;
		}
	}
}